const container = document.getElementById('content-container'), body = document.getElementById('body-container')

const loadJs = url => (new Promise((resolve) => {
    const script = document.createElement('script')
    script.src = url, script.onload = () => resolve(window.Quill), document.head.appendChild(script)
})), loadCss = url => (new Promise((resolve, reject) => {
    const link = document.createElement('link')
    link.rel = 'stylesheet', link.href = url, link.onload = () => resolve(), link.onerror = () => reject(new Error(`加载样式表失败 ${url}`)), document.head.appendChild(link)
}))

const go = async route => {
    let path = window.location.hash.substring(1)

    if (path) {
        /*特殊资源单独引入*/
        switch (path) {
            case '/sys/doc':
                await loadJs('https://thought.ren/js/quill2.0.2.js')
                await loadJs('https://thought.ren/js/katex0.15.2.min.js')
                await loadCss('https://thought.ren/css/quill2.0.2.snow.css')
                await loadCss('https://thought.ren/css/katex0.15.2.min.css')
                break;
        }

        const index = await import('@/view' + path + '/index.html')
        container.innerHTML = index.default

        const form = await import('@/view' + path + '/form.html')
        body.insertAdjacentHTML('beforeend', form.default)

        const js = await import('@/js' + path + '.js')
        js.init()
    }
}

window.onhashchange = async () => go()

go()

